import os
from dotenv import load_dotenv
from flask_script import Manager
from flask_migrate import Migrate, init, migrate, upgrade

import pymysql
pymysql.install_as_MySQLdb()

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path, override=True)

from app import create_app
from app.extensions import db

app = create_app()
manager = Manager(app)
Migrate(app, db)


@manager.command
def db_init():
    """初始化迁移环境"""
    init()


@manager.command
def db_migrate():
    """生成迁移脚本"""
    migrate()


@manager.command
def db_upgrade():
    """应用迁移到数据库"""
    upgrade()


if __name__ == "__main__":
    manager.run()
